package com.cbarmstrong.Finance;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;

public class PlainIndicator implements Indicator {

	@SuppressWarnings("rawtypes")
	private Vector values;
	
	@SuppressWarnings("rawtypes")
	public PlainIndicator(Vector values){
		//Data.getLogger().fine("PlainIndicator: initialising with "+values.size()+" values");	
		this.values=values;
		//Data.getLogger().fine("PlainIndicator: initialised with "+this.values.size()+" values");
	}
	
	@SuppressWarnings("rawtypes")
	public Vector getValues(){ 
		//Data.getLogger().fine("PlainIndicator: Returning "+values.size()+" values... "); 
		return values;
	}

	public Integer position(Date date) {
		return 0;
	}

	@Override
	public int days() {
		return values.size();
	}
	
	public String save(){
		return "Don't Save This";
	}

	@SuppressWarnings("unchecked")
	@Override
	public void toGraph(Ticker ticker, OutputStream out, int x, int y) {
		Vector<Double> i1=this.values;
		TimeSeriesCollection dataset=new TimeSeriesCollection();
		TimeSeries t1=new TimeSeries(1);
		for(int i=0;i<ticker.getDates().size();i++){
			t1.add(new TimeSeriesDataItem(new Day(ticker.getDates().get(i)), i1.get(i)));
		}
		dataset.addSeries(t1);
		JFreeChart chart = ChartFactory.createTimeSeriesChart("PlainInd", "Time", "Values", dataset, false, false, false);
		XYPlot plot = (XYPlot) chart.getPlot();
		plot.setRenderer(new XYSplineRenderer(50));
		for(int i=0;i<plot.getSeriesCount();i++){
			((XYSplineRenderer) plot.getRenderer()).setSeriesShapesVisible(i, false);
			plot.getRenderer().setSeriesPaint(i++, Color.black);
		}
		try {
			ChartUtilities.writeChartAsJPEG(out, chart, x, y);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
